<?php
!defined ('P_W') && exit('Forbidden');
class PW_Video {
	
	function addVideo($hid,$data) {
		global $timestamp;
		$image = $this->_uploadPicture($_FILES,$hid);
		$imagePath = $image ? $image['fileuploadurl'] : '';

		$videoDB = $this->_getVideoDB();
		$temp = $videoDB->insert(array(
			'hid'	=>$hid,
			'address'=>$data['address'],
			'image'	=>$imagePath,
			'uploadtime'	=>$timestamp,
			'descrip'	=>$data['descrip'],
			'creator'	=>$data['creator'],
		));
		if (!$temp) Showmsg('undefined_action');
		//$this->_updateCache($hid);
		return true;
	}
	function updateVideo($id,$data) {
		global $timestamp;
		$videoDB = $this->_getVideoDB();
		$video = $videoDB->get($id);
		if (!$video) return false;
		$updateArray = array(
			'address'=>$data['address'],
			'uploadtime'	=>$timestamp,
			'descrip'	=>$data['descrip'],
			'creator'	=>$data['creator'],
		);
		$image = $this->_uploadPicture($_FILES,$video['hid']);
		if ($image && $image['fileuploadurl']) {
			$this->_unlinkImage($video['image']);
			$updateArray['image'] = $image['fileuploadurl'];
		}
		$videoDB->update($id,$updateArray);
		//$this->_updateCache($video['hid']);
		return true;
	}
	
	function getVideo($id) {
		$videoDB = $this->_getVideoDB();
		$temp = $videoDB->get($id);
		if ($temp) {
			return $this->_initData($temp);
		}
		return false;
	}
	function getVideos($hid,$page=1,$prePage = 14) {
		$videoDB = $this->_getVideoDB();
		$hid = (int) $hid;
		if ($page=='all') {
			$temp = $videoDB->getAllByHid($hid);
		} else {
			$temp = $videoDB->getVideos($hid,$page,$prePage);
		}
		
		foreach ($temp as $id=>$value) {
			$temp[$id] = $this->_initData($value);
		}
		return $temp;
	}
	
	function deleteVideo($id) {
		$videoDB = $this->_getVideoDB();
		$video = $videoDB->get($id);
		if (!$video) return false;
		$this->_unlinkImage($video['image']);
		$videoDB->delete($id);
	}
	
	function deleteVideos($ids) {
		foreach ($ids as $key=>$id) {
			$this->deleteVideo($id);
		}
	}
	
	function getPages($hid,$url,$page,$prePage=14) {
		$videoDB = $this->_getVideoDB();
		$total = $videoDB->countByHid($hid);
		$numofpage = ceil($total/$prePage);
		$numofpage < 1 && $numofpage = 1;
		$page > $numofpage && $page = $numofpage;
		
		return numofpage($total,$page,$numofpage,$url);
	}
	
	function _unlinkImage($image) {
		if (!$image) return false;
		global $attachdir;
		P_unlink($attachdir.'/'.$image);
		P_unlink($this->_getMiniImageUrl($attachdir.'/'.$image));
	}
	
	function _updateCache($hid) {
		$pageCache = getPageCache('info',$hid);
		$temp_config = $pageCache->config;
		foreach ($temp_config as $key=>$value) {
			if ($value['type']=='video') {
				$pageCache->deleteCache($key);
			}
		}
	}
	
	function _uploadPicture($fileArray,$hid) {
		if (!$fileArray) return false;
		require_once(M_P . 'lib/model/videologoupload.class.php');
		$img = new VideoLogoUpload($hid,$this->_getNaturePath(),$this->_getMiniPath());
		PwUpload::upload($img);
		return $img->getAttachs();
	}
	
	function _initData($data) {
		global $db_bbsurl,$houseBaseUrl;
		$data['url'] = $houseBaseUrl.'q=video&id='.$data['id'];
		$data['image'] = $this->_getImageUrl($data['image']);
		$tempAddress = $data['address'];
		$data['address'] = strpos($tempAddress,'http://') === false ? $db_bbsurl.$tempAddress : $tempAddress;
		$data['address'] = stripcslashes($data['address']);
		$data['mini'] = $this->_getMiniImageUrl($data['image']);
		$data['uploadtime'] = get_date($data['uploadtime'],'Y-m-d H:i');
		return $data;
	}
	
	function _getImageUrl($imageUrl) {
		if (!$imageUrl) return 'mode/house/images/video.jpg';
		$temp = geturl($imageUrl);
		if (!$temp) return 'mode/house/images/video.jpg';
		return $temp[0];
	}
	function _getMiniImageUrl($imageUrl) {
		return str_replace($this->_getNaturePath(),$this->_getMiniPath(),$imageUrl);
	}
	
	function _getVideoDB() {
		return House::loadDB('house_video');
	}
	
	function _getNaturePath() {
		return 'house/video/nature/';
	}
	function _getMiniPath() {
		return 'house/video/mini/';
	}
}